package com.ruoyi.uni.web.controller.uni;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.uni.config.uploadConfig;
import com.ruoyi.uni.domain.DcGoods;
import com.ruoyi.uni.service.uniService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/uni")
public class uniController {

    @Autowired
    private uniService uniService;

    @Autowired
    private uploadConfig uploadConfig;

    @PostMapping ("/login")
    public AjaxResult login(@RequestBody JSONObject o){
        return uniService.login(o.getString("jsCode"));
    }

//    @PostMapping("/download")
//    public void downloadQrcode(@RequestBody JSONObject o, HttpServletResponse response){
//        uniService.downloadQrcode(o.getString("path"),o.getInteger("width"),response);
//    }

    @PostMapping("/updateName")
    public AjaxResult updateName(@RequestBody JSONObject o,HttpServletRequest httpServletRequest){
        Long id = uniService.getDcCustomerByToken(httpServletRequest).getId();
        return uniService.updateName(o.getString("name"),id);
    }

    @PostMapping("/updateEmail")
    public AjaxResult updateEmail(@RequestBody JSONObject o,HttpServletRequest httpServletRequest){
        Long id = uniService.getDcCustomerByToken(httpServletRequest).getId();
        return uniService.updateEmail(o.getString("email"),id);
    }
    @PostMapping("/updateSex")
    public AjaxResult updateSex(@RequestBody JSONObject o,HttpServletRequest httpServletRequest){
        Long id = uniService.getDcCustomerByToken(httpServletRequest).getId();
        return uniService.updateSex(o.getLong("sex"),id);
    }
    @PostMapping("/updateTel")
    public AjaxResult updateTel(@RequestBody JSONObject o,HttpServletRequest httpServletRequest){
        Long id = uniService.getDcCustomerByToken(httpServletRequest).getId();
        return uniService.updateTel(o.getString("tel"),id);
    }
    @GetMapping("/userInfo")
    public AjaxResult getUserInfo(HttpServletRequest httpServletRequest){
        Long id = uniService.getDcCustomerByToken(httpServletRequest).getId();
        return uniService.selectCustomerById(id);
    }
    @GetMapping("/typeList")
    public AjaxResult getTypeList(){
        return uniService.getTypeList();
    }

    @PostMapping("/order")
    public AjaxResult submitOrder(@RequestBody JSONObject o,HttpServletRequest httpServletRequest){

        return uniService.submitOrder(o,httpServletRequest);
    }
    @PostMapping("/pay")
    public AjaxResult pay(@RequestBody String orderId){
        return uniService.pay(orderId);
    }
    @PostMapping("/orderDetail")
   public AjaxResult getOrderDetailById(@RequestBody String orderId){
        return uniService.getOrderDetailById(orderId);
    }
    @GetMapping("/order/{status}")
    public AjaxResult  selectOrderByStatus(@PathVariable int status,HttpServletRequest httpServletRequest){
        System.out.println(status);
        return uniService.selectOrderByStatus(status,httpServletRequest);
    }
    @DeleteMapping("/order")
    public AjaxResult cancelOrder(@RequestBody String orderId){
       return uniService.cancelOrder(orderId);
    }
    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = uploadConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
    @PutMapping("/cover")
    public AjaxResult updateCover(@RequestBody String coverUrl,HttpServletRequest httpServletRequest){
        return uniService.updateCover(coverUrl,httpServletRequest);
    }
}
